package LuoGu.搜索.PERKET;

import java.util.Scanner;

public class Main {
    static int ans = Integer.MAX_VALUE;
    static int n;
    static int [][] temp;
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        n = s.nextInt();
        temp = new int [n][2];
        for(int i=0;i<n;i++){
            temp[i][0] = s.nextInt();
            temp[i][1] = s.nextInt();
        }
        int ss = temp[0][0];
        int kk = temp[0][1];
        dfs(1,ss,kk);
        System.out.print(ans);
    }
    public static void dfs(int index,int sd,int kd){
        ans = Math.min(ans,Math.abs(sd-kd));
        if(index==n) return;
        dfs(index+1,sd*temp[index][0],kd+temp[index][1]);
        dfs(index+1,sd*temp[index][0]/temp[index-1][0],kd+temp[index][1]-temp[index-1][1]);
    }
}
